package com.yoyosys.jdbcdatdbase.service.impl;

import com.yoyosys.common.vo.Message;
import com.yoyosys.jdbcdatdbase.po.SqlExecutor;
import com.yoyosys.jdbcdatdbase.service.JdbcConnectDatabaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class JdbcConnectDatabaseServiceImpl implements JdbcConnectDatabaseService {

    private final JdbcTemplate primaryJdbcTemplate;
    private final JdbcTemplate secondaryJdbcTemplate;
    private final JdbcTemplate oracleJdbcTemplate;
    private final JdbcTemplate h2JdbcTemplate;
    private final JdbcTemplate dmJdbcTemplate;
    private final JdbcTemplate sqlServiceJdbcTemplate;

    @Autowired
    public JdbcConnectDatabaseServiceImpl(
            @Qualifier("primaryJdbcTemplate") JdbcTemplate primaryJdbcTemplate,
            @Qualifier("secondaryJdbcTemplate") JdbcTemplate secondaryJdbcTemplate,
            @Qualifier("oracleJdbcTemplate") JdbcTemplate oracleJdbcTemplate,
            @Qualifier("h2JdbcTemplate") JdbcTemplate h2JdbcTemplate,
            @Qualifier("dmJdbcTemplate") JdbcTemplate dmJdbcTemplate,
            @Qualifier("sqlServiceJdbcTemplate") JdbcTemplate sqlServiceJdbcTemplate) {
        this.primaryJdbcTemplate = primaryJdbcTemplate;
        this.secondaryJdbcTemplate = secondaryJdbcTemplate;
        this.oracleJdbcTemplate = oracleJdbcTemplate;
        this.h2JdbcTemplate = h2JdbcTemplate;
        this.dmJdbcTemplate = dmJdbcTemplate;
        this.sqlServiceJdbcTemplate = sqlServiceJdbcTemplate;
    }
    @Override
    public Message executeSql(SqlExecutor sqlExecutor) {
        Message message = new Message();
        try {
          JdbcTemplate target;
            String lowerDsName = sqlExecutor.getDsType().toLowerCase();
            // 添加日志
            log.info("尝试使用数据源: {}", lowerDsName);

          if ("primary".equalsIgnoreCase(lowerDsName)) {
              target = primaryJdbcTemplate;
          } else if ("secondary".equalsIgnoreCase(lowerDsName)) {
              target = secondaryJdbcTemplate;
          } else if ("oracle".equalsIgnoreCase(lowerDsName)){
              target = oracleJdbcTemplate;
          } else if ("h2".equalsIgnoreCase(lowerDsName)){
              target = h2JdbcTemplate;
          } else if ("dm".equalsIgnoreCase(lowerDsName)){
              target = dmJdbcTemplate;
          } else if ("sqlService".equalsIgnoreCase(lowerDsName)){
              target = sqlServiceJdbcTemplate;
          }else {
              throw new IllegalArgumentException("未知数据源: " + sqlExecutor.getDsType());
          }
          // 2. 统一转小写判断
          String lowerSql = sqlExecutor.getSql().toLowerCase().trim();
          if (lowerSql.contains("select") ){
              // 3. 执行查询
              List<Map<String, Object>> result = target.queryForList(sqlExecutor.getSql());
              message.setSuccessMsg("执行成功");
              message.setData(result);
          }else {
              // 情况2：执行非查询语句
              target.execute(sqlExecutor.getSql());
              List<Map<String, String>> maps = Collections.singletonList(
                      Collections.singletonMap("rows_affected", "SQL执行成功")
              );
              message.setSuccessMsg("执行成功");
              message.setData(maps);
          }
      } catch (Exception e) {
            log.error("executeSql error:{}",e);
            message.setErrorMsg("执行失败:"+e.getMessage());
      }
        return message;
    }

}
